package cc.vv.btpartyjob.partyservice.activity

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.util.Log
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.binder.BaseBinder
import cc.vv.btpartyjob.partyservice.R
import cc.vv.btpartyjob.partyservice.delegate.TinyWishDetailActivityDelegate
import cc.vv.btpartyjob.partyservice.globle.BroadCastServiceKey
import cc.vv.btpartyjob.partyservice.globle.IntentServiceKey
import cc.vv.btpartyjob.partyservice.module.resp.TinyWishDetailResponseObj
import cc.vv.btpartyjob.partyservice.server.DataPartyServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 *微心愿详情
 * Created by Chen.qi on 2018/11/7.
 */
class TinyWishDetailActivity : BaseActivityMVP<TinyWishDetailActivityDelegate>() {
    /**
     * 详情的id
     */
    private var id: String = ""

    /**
     * 认领内容
     */
    private var content: String? = ""
    /**
     * 认领人
     */
    private var personName: String? = ""


    /**
     * 刷新数据的广播
     */
    private var mMyBroadCast: MyBroadCast? = null

    /**
     * 刷选器
     */
    private var filter: IntentFilter? = null

    /**
     * 初始化广播
     */
    private fun initBroadcast() {
        if (null == mMyBroadCast) {
            mMyBroadCast = MyBroadCast()
            filter = IntentFilter()
            filter?.addAction(BroadCastServiceKey.BROADCAST_REFRESH_OR_FINISH_TINY_WISH)
            this.registerReceiver(mMyBroadCast, filter)
        }
    }


    /**
     * 广播接收
     */
    private inner class MyBroadCast : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent?) {
            //接收头像修改广播，发送对应的消息
            if (intent?.action == BroadCastServiceKey.BROADCAST_REFRESH_OR_FINISH_TINY_WISH) {
                Log.e("lk_chen", "result==" + "收到广播刷新数据");
                finish()
            }
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        if (null != mMyBroadCast) {
            unregisterReceiver(mMyBroadCast)
        }
    }

    override fun initData() {
        initBroadcast()
        viewDelegate?.setTopBarTitle("心愿详情")
        id = intent.getStringExtra(IntentServiceKey.INTENT_KEY_TINY_DETAIL_ID)
        DataPartyServer.tinyWishDetailHttp(id, true)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //认领心愿
            val intent = Intent()
            intent.setClass(this, ClaimWishActivity::class.java)
            intent.putExtra(IntentServiceKey.INTENT_KEY_TINY_DETAIL_ID, id)
            intent.putExtra(IntentServiceKey.INTENT_KEY_RL_CONTENT, content)
            intent.putExtra(IntentServiceKey.INTENT_KEY_RL_PER_NAME, personName)
            startActivity(intent)

        }, R.id.rl_atwD_tinyWishRL)
    }

    override fun getDelegateClass(): Class<TinyWishDetailActivityDelegate> {
        return TinyWishDetailActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }


    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is TinyWishDetailResponseObj -> {
                val detailObj = obj.data
                content = detailObj?.wishConetnt
                personName = detailObj?.name
                viewDelegate?.setTinyName(detailObj?.name)
                viewDelegate?.setTinyTel(detailObj?.mobile)
                viewDelegate?.setTinyAddress(detailObj?.addr)
                viewDelegate?.setTinyContent(detailObj?.wishConetnt)
                try {
                    val startTime = detailObj?.startTime?.substring(0, 10)
                    val endTime = detailObj?.endTime?.substring(0, 10)
                    viewDelegate?.setTinyTime("$startTime 至$endTime")
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                if (1 == detailObj?.claimState) {
                    viewDelegate?.setTinyStatus("已认领", 1)
                    viewDelegate?.setSubmitButtonShow(false)
                } else {
                    viewDelegate?.setTinyStatus("未认领", 0)
                    viewDelegate?.setSubmitButtonShow(true)
                }
            }
        }
    }

}